<?php
/**
住户
 */


namespace App\Http\Controllers\Property;


use App\Common\json;
use App\Common\SmsClient;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ResidentController extends Controller{



    //添加新住户
    public function AddResident(Request $request){
        if($request ->isMethod('post')){
            $arr           = (object)array();
            $user_resident       = $request ->input('user_id');
            $myaddress_resident   = $request ->input('myaddress_id');
            $resident_name        = $request ->input('name');
            $resident_phone       = $request ->input('phone');
            $resident_type        = $request ->input('resident_type');
            $select               = DB::table('myresident')
                                  ->where('user_resident',$user_resident)
                                  ->where('myaddress_resident',$myaddress_resident)
                                  ->where('resident_name',$resident_name)
                                  ->where('resident_phone',$resident_phone)
                                  ->where('resident_type',$resident_type)
                                  ->get();
            if($select == true){
                return json::json('failure', '添加的数据已经存在', $arr);
            }else{
                $data = DB::table('myresident')
                    ->insert([
                        'user_resident' => $user_resident,
                        'myaddress_resident' => $myaddress_resident,
                        'resident_name'                =>$resident_name,
                        'resident_phone'           =>$resident_phone,
                        'resident_type'                  =>$resident_type,
                        'resident_create_time'          =>date('Y-m-d H:i:s')
                    ]);
                if ($data == true) {
                    return json::json('success', '添加成功', $arr);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', $arr);
                }
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //修改住户
    public function UpdateResident(Request $request){
        if($request ->isMethod('post')){
            $arr           = (object)array();
            $resident_id          = $request ->input('resident_id');
            $resident_name        = $request ->input('name');
            $resident_phone       = $request ->input('phone');
            $resident_type        = $request ->input('resident_type');
            $data = DB::table('myresident')
                ->where('resident_id',$resident_id)
                ->update([
                    'resident_name'                =>$resident_name,
                    'resident_phone'           =>$resident_phone,
                    'resident_type'                  =>$resident_type
                ]);
            if ($data == true) {
                return json::json('success', '修改成功', $arr);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', $arr);
            }
        }
    }

    //删除
    public function DelResident(Request $request){
        if($request ->isMethod('post')){
            $arr           = (object)array();
            $resident_id          = $request ->input('resident_id');
            $data = DB::table('myresident')
                ->where('resident_id',$resident_id)
                ->delete();
            if ($data == true) {
                return json::json('success', '删除成功', $arr);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', $arr);
            }
        }
    }

    //public function SelectResident(Request $request){}


}
